home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
cnews014.zip
/
PAINTSRC.ZIP
/
PAINT.C
Wrap
C/C++ Source or Header
|
1989-02-20
|
34KB
|
1,302 lines
/* PAINT.C
*
* Scott R. Houck
* Written in Turbo C 2.0
*
* Add the EGAVGA.BGI driver to graphics.lib using the BGIOBJ program.
*
* Compile with: tcc -ml paint graphics.lib
*
* This is a simple paint program
*/
#include <stdio.h>
#include <math.h>
#include <dos.h>
#include <mem.h>
#include <alloc.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <ctype.h>
#include <graphics.h>
/* Mouse buttons */
#define LEFTBUTTON 1
#define RIGHTBUTTON 2
/* Clipping */
#define CLIPPING_ON 1
#define CLIPPING_OFF 0
/* Boolean values */
#define TRUE 1
#define FALSE 0
/* Dialog box options */
#define YES 1
#define NO 0
/* Menu choices */
#define IGNORE -1
#define PAINT 0
#define FILL 1
#define ERASER 2
#define CLEAR 3
#define SAVE 4
#define LOAD 5
#define QUIT 6
#define RUBBER_LINE 7
#define RUBBER_RECTANGLE 8
#define LINE 0
#define RECTANGLE 1
/* Some useful typedefs */
typedef struct pointtype POINT; /* A point with x and y as ints */
typedef POINT EXTENT [2]; /* Rectangular extent */
/* Function prototypes */
void InitializeGraphics(void);
void DrawScreen(void);
int MouseReset(int *);
void MouseOn(void);
void MouseOff(void);
int MouseStatus(POINT *);
void MouseWaitForPress(int, POINT *);
void MouseWaitForRelease(int, POINT *);
void MouseSetCursor(int [16][2]);
void HighlightMenu(int, int, int);
int PointInExtent(POINT, EXTENT);
void HandleDraw(POINT);
int PickCorrInMenu(POINT);
int PickCorrInColor(POINT);
int PickCorrInPattern(POINT);
void HandlePick(POINT);
void SetForeground(int);
void SetBackground(int);
void SetPattern(int);
void DoPaint(POINT);
void DoFill(POINT);
void DoEraser(POINT);
void DoClear(void);
void DoSave(void);
void DoLoad(void);
void DoRubber(int, POINT);
int DialogBox(void);
void Beep(void);
void LowBeep(void);
int GetChar(char *, char *, int *);
char * GetString(char *, int, int, int, char *, char *, char *, int,
int (*)(int, int));
int PromptForFilename(char *);
/* Global variables */
int maxx, maxy; /* Maximum pixel values */
int dminx, dminy, dmaxx, dmaxy; /* Drawing area coordinates */
int bgminx, bgminy, bgmaxx, bgmaxy; /* Background box */
int fgminx, fgminy, fgmaxx, fgmaxy; /* Foreground box */
int rl1x, rl1y, rl2x, rl2y; /* Rubber line coordinates */
int rr1x, rr1y, rr2x, rr2y; /* Rubber rectangle coordinates */
int xminx, xminy, xmaxx, xmaxy; /* Dialog box coordinates */
EXTENT drawExtent; /* Extent of the drawing area */
EXTENT colorExtent[16]; /* Extents of the 16 color boxes */
EXTENT patternExtent[11]; /* Extents of the 11 patterns */
EXTENT yesExtent, noExtent; /* Extent of the YES/NO buttons */
int currentColor; /* Current foreground color */
int fillPattern; /* Current fill pattern */
int fillColor; /* Current fill color */
unsigned dialogSize; /* Dialog box image size */
unsigned drawSize; /* Drawing size */
unsigned promptSize; /* Size of prompt box */
void *dialogBuffer, *saveBuffer; /* Dialog box and screen buffers */
void *promptBuffer, *drawBuffer; /* Prompt box and drawing buffers */
int fminx, fminy, fmaxx, fmaxy; /* File prompt box coords */
int promptx, prompty; /* File prompt x and y coords */
struct {
EXTENT extent;
char *text;
} menuBox[10]; /* menu buttons */
int mode; /* current mode */
/* The following two arrays define the eraser-type cursor and the
* regular arrow cursor for use in the MouseSetCursor() routine.
*/
int eraser [16][2] = {
{ 0xFFFF, 0xFFFF }, /* screen mask */
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0xFFFF },
{ 0xFFFF, 0x8001 }, /* cursor mask */
{ 0x8001, 0x8001 },
{ 0x8001, 0x8001 },
{ 0x8001, 0xFFFF },
{ 0x0000, 0x0000 },
{ 0x0000, 0x0000 },
{ 0x0000, 0x0000 },
{ 0x0000, 0x0000 }
};
int cursor [16][2] = {
{ 0x3FFF, 0x1FFF }, /* screen mask */
{ 0x0FFF, 0x07FF },
{ 0x03FF, 0x01FF },
{ 0x00FF, 0x007F },
{ 0x003F, 0x001F },
{ 0x01FF, 0x10FF },
{ 0x30FF, 0xF87F },
{ 0xF87F, 0xFC7F },
{ 0x0000, 0x4000 }, /* cursor mask */
{ 0x6000, 0x7000 },
{ 0x7800, 0x7C00 },
{ 0x7E00, 0x7F00 },
{ 0x7F80, 0x7C00 },
{ 0x6C00, 0x4600 },
{ 0x0600, 0x0300 },
{ 0x0300, 0x0000 }
};
main()
{
POINT position;
int buttonPressed;
InitializeGraphics();
DrawScreen();
MouseOn();
HighlightMenu(mode = PAINT, LIGHTGRAY, BLACK);
while (mode != QUIT)
{
do
{
buttonPressed = MouseStatus(&position);
if (mode == ERASER && PointInExtent(position, drawExtent))
MouseSetCursor(eraser);
else
MouseSetCursor(cursor);
}
while (!(buttonPressed & (RIGHTBUTTON | LEFTBUTTON)));
if (PointInExtent(position, drawExtent))
HandleDraw(position);
else
HandlePick(position);
}
MouseOff();
closegraph();
}
/* InitializeGraphics() initializes the graphics package and mouse driver.
* If there is an error, the program aborts with an appropriate error
* message.
*/
void InitializeGraphics()
{
int graphDriver, graphMode, errorCode;
int numberOfButtons;
if (MouseReset(&numberOfButtons) == 0)
{
printf("Mouse driver is not installed\n");
exit(1);
}
if (numberOfButtons < 2)
{
printf("This program requires a mouse with at least two buttons\n");
exit(2);
}
if (registerbgidriver(EGAVGA_driver) < 0)
exit(1);
graphDriver = DETECT;
initgraph(&graphDriver, &graphMode, "");
errorCode = graphresult();
if (errorCode != grOk)
{
printf("Graphics System Error: %s\n", grapherrormsg(errorCode));
exit(1);
}
/* Get maximum x and y screen coordinates */
maxx = getmaxx();
maxy = getmaxy();
}
void DrawScreen()
{
int i;
int cminx, cminy, cmaxx, cmaxy; /* color box coordinates */
int pminx, pminy, pmaxx, pmaxy; /* pattern box coordinates */
int cwidth; /* width of color box */
int cheight; /* height of color box */
int pwidth; /* width of pattern box */
int gap; /* gap between various objects */
int bminx, bminy, bmaxx, bmaxy; /* Menu buttons */
int bwidth, bheight, bgap, bstart; /* Menu buttons */
int dwidth, dheight; /* Drawing area dimensions */
int xwidth, xheight; /* Dialog box dimensions */
int yminx, yminy, ymaxx, ymaxy; /* YES button */
int nminx, nminy, nmaxx, nmaxy; /* NO button */
int fwidth, fheight; /* Height of file prompt box */
static char *boxText[] = { "PAINT", "FILL", "ERASER", "CLEAR", "SAVE",
"LOAD", "QUIT", "", "" };
setvisualpage(0);
setactivepage(0);
/* Draw the main background */
setcolor(WHITE);
rectangle(0, 0, maxx, maxy);
setfillstyle(INTERLEAVE_FILL, CYAN);
floodfill(1, 1, WHITE);
/* Draw the drawing area */
drawExtent[0].x = dminx = (int)(0.30 * maxx);
drawExtent[0].y = dminy = (int)(0.05 * maxy);
drawExtent[1].x = dmaxx = (int)(0.95 * maxx);
drawExtent[1].y = dmaxy = (int)(0.80 * maxy);
drawSize = imagesize(dminx, dminy, dmaxx, dmaxy);
drawBuffer = malloc(drawSize);
dwidth